﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace STLGROUP.Layers.Presentations.FormBase
{
    public partial class frmExportExcel : frmBase
    {

        #region Variables

        private string _sheetName;
        private DataGridView _dgv;

        #endregion

        #region Constructors

        public frmExportExcel(string sheetName,DataGridView dgv)
        {
            InitializeComponent();

            _sheetName = sheetName;
            _dgv = dgv;
        }

        public frmExportExcel(DataGridView dgv)
        {
            InitializeComponent();

            _sheetName = "Sheet1";
            _dgv = dgv;
        } 

        #endregion

        #region Private Methods

        #region LoadData

        private void LoadData()
        {
            cbxExtension.Items.Add(".xlsx");
            cbxExtension.Items.Add(".xls");
            cbxExtension.SelectedIndex = 0;
            txtFileName.Text = _dgv.Name;
        }

        #endregion

        #region Browser

        private void Browser()
        {
            FolderBrowserDialog open = new FolderBrowserDialog();
            DialogResult result = open.ShowDialog();

            if (result == DialogResult.OK)
            {
                txtFolder.Text = open.SelectedPath;

                txtFileName.ReadOnly = false;
                txtFileName.Text = string.Empty;
                cbxExtension.Enabled = true;
                btxExportExcel.Enabled = true;
                btxReset.Enabled = true;
            }
        }

        #endregion

        #region Reset

        private void Reset()
        {
            txtFolder.Text = "Chọn đường dẫn thư mục";
            txtFileName.Text = "Nhập tên file Excel lưu trữ";
            txtFileName.ReadOnly = true;
            cbxExtension.Enabled = false;
            btxExportExcel.Enabled = false;
            btxReset.Enabled = false;
        }

        #endregion

        #region ExportExcel

        private void ExportExcel(string folder, string fileName, string extension)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    MessageBox.Show("Vui lòng nhập vào tên file Excel", "Lỗi");
                    return;
                }
                else
                {
                    string pathFile = folder + "\\" + fileName + extension;

                    Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                    Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                    Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

                    worksheet = workbook.Sheets["Sheet1"];
                    worksheet = workbook.ActiveSheet;
                    worksheet.Name = _sheetName;

                    for (int i = 1; i < _dgv.Columns.Count + 1; i++)
                    {
                        worksheet.Cells[1, i] = _dgv.Columns[i - 1].HeaderText;
                    }

                    for (int i = 0; i < _dgv.Rows.Count; i++)
                    {
                        for (int j = 0; j < _dgv.Columns.Count; j++)
                        {
                            if (_dgv.Rows[i].Cells[j].Value != null)
                            {
                                worksheet.Cells[i + 2, j + 1] = _dgv.Rows[i].Cells[j].Value.ToString();
                            }
                        }
                    }
                    workbook.SaveAs(pathFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);


                    DialogResult result = MessageBox.Show("Xuất dữ liệu ra file Excel thành công.\nHãy chọn 'Yes' để mở file Excel vừa tạo","Xác nhận",
                                                            MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        app.Visible = true;
                    }
                    else
                    {
                        app.Quit();
                    }
                    this.Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Xuất dữ liệu ra file Excel không thành công.\n Vui lòng kiểm tra lại thông tin file Excel", "Lỗi");
            }            
        }

        #endregion 

        #endregion

        #region Events

        private void frmExportExcel_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btxBrowser_Click(object sender, EventArgs e)
        {
            Browser();
        }

        private void btxExportExcel_Click(object sender, EventArgs e)
        {
            ExportExcel(txtFolder.Text, txtFileName.Text, cbxExtension.Text);
        }

        private void btxReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void btxExit_Click(object sender, EventArgs e)
        {
            this.Close();
        } 

        #endregion
      
    }
}
